﻿using System;
using System.Linq;
using System.Web;
using Summer.Core.Ioc;

#region License
/*
 *  Copyright (c) Dumuzi.cn 2009 , All rights reserved.
 *  CLR Version: 2.0.50727.4927
 *  NameSpace: Summer.Web.Support
 *  Author:Edwin Tai,戴佳顺
 *  Msn:edwin19861218@hotmail.com
 *  QQ: 1961218
 *  Date:2009/10/29 19:57:55
 *  Licensed under the Microsoft Public License (Ms-PL)
 *  This license governs use of the accompanying software. 
 *  If you use the software, you accept this license.
 *  If you do not accept the license, do not use the software.
 */
#endregion

namespace Summer.Web.Support
{
    /// <summary>
    /// The Entity class of IHandlerFactory
    /// </summary>
    public class WebHandlerFactory : AbstractHandlerFactory
    {
        /// <summary>
        /// Create instance or load old one
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="url"></param>
        /// <param name="pathTranslated"></param>
        /// <returns></returns>
        public override IHttpHandler GetHandler(System.Web.HttpContext context, string requestType, string url, string pathTranslated)
        {
            return base.GetHandler(context, requestType, url, pathTranslated);
        }
        public override void ReleaseHandler(IHttpHandler handler)
        {

        }
        /// <summary>
        /// The method to create instance with HttpContext
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="url"></param>
        /// <param name="pathTranslated"></param>
        /// <returns></returns>
        protected override IHttpHandler CreateHttpHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            /* Create instance without HttpContext
            Page page = BuildManager.CreateInstanceFromVirtualPath(url, typeof(Page)) as Page;
            return page;
             */
            return base.CreateHttpHandler(context, requestType, url, pathTranslated);
        }
        /// <summary>
        /// Create instance and set properity
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="url"></param>
        /// <param name="pathTranslated"></param>
        /// <returns></returns>
        protected override IHttpHandler CreateHandlerWithProperities(HttpContext context, string requestType, string url, string pathTranslated)
        {
            Summer.Core.Ioc.Context configContext = context.Application.GetConfigContextContainer();
            IContextObject contextObject = null;
            if (!configContext.TryGetValue(url, out contextObject))
            {
                //The contextObject cannot be found in context
                //So the page handler should be created everytime
                return this.CreateHttpHandler(context, requestType, url, pathTranslated);
            }
            //Create instance page with the Context
            IHttpHandler handler = (IHttpHandler)this.GetInstanceWithContextObjectByRef(contextObject, context, requestType, url, pathTranslated);
            return handler;
        }
        /// <summary>
        /// Get ContextObject by Ref string and create new instance with properities
        /// </summary>
        /// <param name="contextObject">The ContextObject about the request</param>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="url"></param>
        /// <param name="pathTranslated"></param>
        /// <returns></returns>
        private object GetInstanceWithContextObjectByRef(IContextObject contextObject, HttpContext context, string requestType, string url, string pathTranslated)
        {
            Func<object> createNewInstance = delegate
            {
                object lib = this.CreateHttpHandler(context, requestType, url, pathTranslated);
                if (lib != null && contextObject.Property != null && contextObject.Property.Count() != 0)
                {
                    ContextManager.SetPropertyInContextObject(contextObject, context.Application.GetDefaultContextContainer(), lib);
                }
                return lib;
            };
            return createNewInstance();
            /*
            //Load instance
            //FIXME the instance getter error
            //The Application Object in Context which in Page is null
            //How to set it?
            
            if (!contextObject.Singleton)
            {
                //Create instance
                contextObject.Object = createNewInstance();
                return contextObject.Object;
            }
            else
            {
                if (contextObject.Object != null)
                {
                    return contextObject.Object;
                }
                contextObject.Object = createNewInstance();
                return contextObject.Object;
             
            }
             */

        }
    }
}
